using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;
using Nop.Core.Caching;
using Nop.Core.Configuration;
using Nop.Core.Data;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Data;
using Nop.Plugin.Widgets.Carousel.Controllers;
using Nop.Plugin.Widgets.Carousel.Data;
using Nop.Plugin.Widgets.Carousel.Domain;
using Nop.Plugin.Widgets.Carousel.Services;
using Nop.Web.Framework.Mvc;

namespace Nop.Plugin.Widgets.Carousel.Infrastructure
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        // <summary>
        /// Register services and interfaces
        /// </summary>
        /// <param name="builder">Container builder</param>
        /// <param name="typeFinder">Type finder</param>
        /// <param name="config">Config</param>
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            //we cache presentation models between requests
            builder.RegisterType<WidgetsCarouselController>()
                .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("nop_cache_static"));

            builder.RegisterType<CarouselService>().As<ICarouselService>().InstancePerHttpRequest();
            
            //data context
            this.RegisterPluginDataContext<CarouselObjectContext>(builder, "nop_object_context_carousel");

            //override required repository with our custom context
            builder.RegisterType<EfRepository<CarouselObject>>()
                .As<IRepository<CarouselObject>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_carousel"))
                .InstancePerHttpRequest();
            builder.RegisterType<EfRepository<CarouselEntity>>()
               .As<IRepository<CarouselEntity>>()
               .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_carousel"))
               .InstancePerHttpRequest();
        }

        public int Order
        {
            get { return 2; }
        }
    }
}
